
import os
import sys

sys.path += [os.path.join(r'../tools/')]

from compiler import *


APP_NAME    = 'start'

APP_MAP     = APP_NAME + '.map'
APP_LINK    = 'link.lds'


# tool = GetCompiler(CPU.M4, ARGUMENTS.get('debug',0), bare = True)
# tool = GetCompiler(CPU.A53, ARGUMENTS.get('debug',0), bare = True)
tool = GetCompiler(CPU.A7, ARGUMENTS.get('debug',0), bare = True)

env = Environment(
    AS   = tool['AS'],   ASFLAGS = tool['AFLAGS'],
    CC   = tool['CC'],   CCFLAGS = tool['CFLAGS'],
    CXX  = tool['CXX'],  CXXFLAGS = tool['CFLAGS'],
    AR   = tool['AR'],   ARFLAGS = '-rc',
    LINK = tool['LINK'], LINKFLAGS = tool['LFLAGS'] + ' -Wl,-Map=' + APP_MAP + ' -T ' + APP_LINK
)

env['PROGSUFFIX'] = '.elf'
env.PrependENVPath('PATH', tool['EXEC_PATH'])

# print(env.Dump())

APP_TARGET  = APP_NAME + env['PROGSUFFIX']
APP_BIN     = APP_NAME + '.bin'
APP_DUMP    = APP_NAME + '.asm'
APP_ELF_INFO= APP_NAME + '.info'


env.Clean(APP_TARGET, APP_MAP)
env.Clean(APP_TARGET, APP_BIN)
env.Clean(APP_TARGET, APP_DUMP)
env.Clean(APP_TARGET, APP_ELF_INFO)


# srcs = Glob('*.s')
srcs = Glob('*.c') + Glob('*.s')

env.Program(APP_TARGET, srcs , CPPPATH = './')

env.AddPostAction(APP_TARGET,
    tool['OBJCPY'] + ' -O binary $TARGET ' + APP_NAME + '.bin\n'
    + tool['SIZE'] + ' $TARGET \n'
    + tool['OBJDUMP'] + ' -D $TARGET > ' + APP_DUMP + '\n'
    + tool['READELF'] + ' -a $TARGET > ' + APP_ELF_INFO + '\n'
    )